package com.itheima.controller;

import com.aliyun.utils.AliyunOSSUtils;
import com.itheima.anno.Log;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 处理文件上传请求
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    AliyunOSSUtils aliyunOSSUtils;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @Log
    public R upload(MultipartFile file) throws IOException {
        //MultipartFile对象：表示客户端上传过来的临时文件
        //System.out.println("原始文件名：" + file.getOriginalFilename());
        //System.out.println("文件输入流：" + file.getInputStream());
        //将临时文件保存到指定位置
        //file.transferTo(new File("d:/" + file.getOriginalFilename()));

        log.info("文件上传:{}", file);
        //调用工具类，将文件上传到OSS，并返回文件的地址
        String url = aliyunOSSUtils.upload(file);


        return R.ok(url);
    }

}
